<?php

App::uses('Crud', 'Model');

class UserTeam extends Crud
{
    const STATUS_DISABLED = 0;
    const STATUS_ENABLED = 1;
    const TYPE_NORMAL = 1;
    const TYPE_LEADER = 2;

    /**
     * Obtenemos los integrantes de un equipo
     * @param type $teamId
     */
    public function getTeam($teamId) {
        $filters = array(
            'fields' => array(
                'UserTeam.id',
                'UserTeam.user_id',
                'UserTeam.type',
            ),
            'joins' => array(
                array(
                    'table' => 'us3rs_r4gg14n4',
                    'alias' => 'User',
                    'type' => 'INNER',
                    'conditions' => array(
                        'User.id = UserTeam.user_id',
                    ),
                ),
            ),
            'conditions' => array(
                'UserTeam.team_id' => $teamId,
                'UserTeam.status' => self::STATUS_ENABLED,
            ),
        );
        $records = $this->flatArray($this->find('all', $filters));
        return $records;
    }

    static public function getTypes() {
        return array(
            array('text' => 'Asociado', 'value' => self::TYPE_NORMAL),
            array('text' => 'Preparador', 'value' => self::TYPE_LEADER),
        );
    }

    public function deleteMember($teamId, $userId = null) {
        $filters = array(
            'fields' => array(
                'id',
                '"' . self::STATUS_DISABLED . '" as status',
            ),
            'conditions' => array(
                'team_id' => $teamId,
                'status' => self::STATUS_ENABLED,
            ),
        );
        if ($userId !== null) {
            $filters['conditions']['UserTeam.user_id'] = $userId;
        }
        $records = $this->flatArray($this->find('all', $filters));
        $this->saveAll($records, array(
            'fieldList' => array(
                'UserTeam' => array('status'),
        )));
    }

}
